$(function () {
    findByItem(1);
})

function findByItem(pageNO) {
    $.post("/saleOrder/findByItem","pageNO="+pageNO+"&"+$("#findForm").serialize(),function (pageList) {
        var str = "";
        for(var i=0;i<pageList.list.length;i++){
            var obj = pageList.list[i];
            str += `<tr><td>${obj.customerName}</td>
                    <td>${obj.saller}</td>
                    <td>${obj.saleDate}</td>
                    <td>${obj.amount}</td>
                    <td><a class="layui-btn layui-btn-warm" href="javascript:findById(${obj.id})">查看出库详情</a></td></tr>`;
        }
        $("#infoData").html(str);

        if(pageNO == 1){
            layui.use('laypage', function(){
                var laypage = layui.laypage;
                //执行一个laypage实例
                laypage.render({
                    elem: 'cutPageDiv', //注意，这里的 test1 是 ID，不用加 # 号
                    count: pageList.total, //数据总数，从服务端得到
                    limit:pageList.pageSize,
                    jump:function (obj,first) {
                        if(!first){
                            findByItem(obj.curr)
                        }
                    }
                })
            })
        };
    })
}

function findById(id) {

    $.post("/saleOrder/findById","orderId="+id,function (obj) {
        $("#clientSpan").html(obj.customerName);
        $("#sallerSpan").html(obj.saller);
        $("#saleDateSpan").html(obj.saleDate);
        $("#amountSpan").html(obj.amount);
        $("#address").html(obj.address);
        var str = "";
        for(var i=0;i<obj.saleInfoList.length;i++){
            var infoObj = obj.saleInfoList[i];
            str += `<tr><td>${infoObj.goods.name}</td>
                    <td>${infoObj.goods.factory}</td>
                    <td>${infoObj.goods.salePrice}</td>
                    <td>${infoObj.num}</td></tr>`;
        }
        $("#commodityData").html(str);
    })
    
    layui.use('layer', function(){
        var layer = layui.layer;
        layer.open({
            title:"添加出库商品",
            type:1,
            shadeClose:true,
            skin:'layui-layer-rim',
            area:['800px','600px'],
            anim:4,
            content:$("#infoDiv")
        });
    });

}